﻿using System;

namespace Framework.Core.Condition
{
    public class LogicOperation : IOperation
    {
        private LogicType _Type;

        private LogicType Type
        {
            get
            {
                return _Type;
            }
            set
            {
                if (this._Type != value)
                    this._Type = value;
            }
        }

        public LogicOperation(LogicType type)
        {
            this._Type = type;
        }

        public bool Calculate(IExpression leftExpression, IExpression rightExpression)
        {
            if (leftExpression == null || rightExpression == null)
            {
                return false;
            }
             
            switch (this.Type)
            {
                case LogicType.LogicAnd:
                    return leftExpression.GetResult() && rightExpression.GetResult();
                case LogicType.LogicOr:
                    return leftExpression.GetResult() || rightExpression.GetResult();
                default:
                    return false;
            }
        }

        public bool Calculate(IExpressionItem leftExpressionItem, IExpressionItem rightExpressionItem)
        {
            if (leftExpressionItem == null || rightExpressionItem == null)
            {
                return false;
            }

            BoolenExpressionItem ei1 = (BoolenExpressionItem)leftExpressionItem;
            BoolenExpressionItem ei2 = (BoolenExpressionItem)rightExpressionItem;
            switch (this.Type)
            {
                case LogicType.LogicAnd:
                    return ei1.Item && ei2.Item;
                case LogicType.LogicOr:
                    return ei1.Item || ei2.Item;
                default:
                    return false;
            }
        }

        public bool Calculate(IExpression leftExpression, IExpressionItem rightExpressionItem)
        {
            if (leftExpression == null || rightExpressionItem == null)
            {
                return false;
            }

            BoolenExpressionItem ei2 = (BoolenExpressionItem)rightExpressionItem;
            switch (this.Type)
            {
                case LogicType.LogicAnd:
                    return leftExpression.GetResult() && ei2.Item;
                case LogicType.LogicOr:
                    return leftExpression.GetResult() || ei2.Item;
                default:
                    return false;
            }
        }

        public bool Calculate(IExpressionItem leftExpressionItem, IExpression rightExpression)
        {
            if (leftExpressionItem == null || rightExpression == null)
            {
                return false;
            }

            BoolenExpressionItem ei1 = (BoolenExpressionItem)leftExpressionItem;
            switch (this.Type)
            {
                case LogicType.LogicAnd:
                    return ei1.Item && rightExpression.GetResult();
                case LogicType.LogicOr:
                    return ei1.Item || rightExpression.GetResult();
                default:
                    return false;
            }
        }
    }
}
